package net.nyancofc.piconnow;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.location.LocationProvider;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.RemoteException;
import android.util.Log;
import android.widget.Toast;

public class PiconNowGPSManageService
	extends Service
	implements LocationListener {

	private static final String TAG = "PiconNow(GPS)";

	private Location lastLocation;

    private Handler handler=new Handler();
    private boolean running=false;
    boolean useDummyLocation = true;

    LocationManager mLocationManager = null;

    Thread thread;

    //サービス生成時に呼ばれる
    @Override
    public void onCreate() {
        toast(PiconNowGPSManageService.this,"onCreate called!");
        super.onCreate();
		mLocationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
		running = true;
    }

    //サービス開始時に呼ばれる
    @Override
    public void onStart(Intent intent,int startID) {
        super.onStart(intent,startID);

//        //ノティフィケーションの表示
//        showNotification(this,R.drawable.icon,
//            "Picon Nowを開始しました",
//            "Picon Now",
//            "Picon Now管理画面を開きます");

        if (thread != null) {
        	thread.stop();
        }
        registRequest();
        thread=new Thread(){
            public void run() {
                running=true;
                while (running) {
                    handler.post(new Runnable(){
                        public void run() {
//                            toast(PiconNowGPSManageService.this,message);
                            if (lastLocation != null) {
//                            	toast(PiconNowGPSManageService.this,"GPSManage:" + String.valueOf(lastLocation.getAccuracy()));
                            	Log.i(TAG,"get location/accu:" + String.valueOf(lastLocation.getAccuracy()));
                            }
                        }
                    });
                    try {
                        Thread.sleep(5000);
                    } catch (Exception e) {
                    }
                }
            }
        };
        thread.start();
    }

    //サービス停止時に呼ばれる
    @Override
    public void onDestroy() {
        running=false;
        removeRequest();
        super.onDestroy();
    }

    //サービス接続時に呼ばれる
    @Override
    public IBinder onBind(Intent intent) {
        return IPiconNowGPSManageService;
    }

    //トーストの表示　
    private static void toast(Context context,String text) {
        Toast.makeText(context,text,Toast.LENGTH_SHORT).show();
    }

	public void registRequest() {
        if (mLocationManager != null) {
            mLocationManager.requestLocationUpdates(
                    LocationManager.GPS_PROVIDER,
                    0,
                    0,
                    this);
        }
    }

    public void removeRequest() {
        if (mLocationManager != null) {
            mLocationManager.removeUpdates(this);
        }
    }


    public void onProviderDisabled(String provider) {

    }

    public void onProviderEnabled(String provider) {

    }

    // GPSデバイス状態更新イベントハンドラ
    public void onStatusChanged(String provider, int status, Bundle extras) {
        switch (status) {
        case LocationProvider.AVAILABLE:
            Log.v("Status", "AVAILABLE");
            break;
        case LocationProvider.OUT_OF_SERVICE:
            Log.v("Status", "OUT_OF_SERVICE");
            break;
        case LocationProvider.TEMPORARILY_UNAVAILABLE:
            Log.v("Status", "TEMPORARILY_UNAVAILABLE");
            break;
        }
    }

    // 位置情報更新イベントハンドラ
    public synchronized void onLocationChanged(Location location) {
    	if (lastLocation == null) {
    		lastLocation = location;
    	} else if (lastLocation.getAccuracy() >= location.getAccuracy()) {
    		lastLocation = location;
    	}
    }
    //サービスバインダ
    private final IPiconNowGPSManageService.Stub IPiconNowGPSManageService=new IPiconNowGPSManageService.Stub() {
        public Location getGPSInfo() throws RemoteException {
        	return lastLocation;
       }
    };
}
